package com.baofu.paycore.manager.storer.impl;

import com.baofu.paycore.dal.mapper.FreezeMapper;
import com.baofu.paycore.dal.model.FreezeDO;
import com.baofu.paycore.dal.model.PreAuthDO;
import com.baofu.paycore.dal.model.SubPreAuthDO;
import com.baofu.paycore.dal.model.base.BaseDO;
import com.baofu.paycore.manager.converter.BO2DOConverter;
import com.baofu.paycore.manager.model.FreezeBO;
import com.baofu.paycore.manager.model.preAuth.PreAuthBO;
import com.baofu.paycore.manager.storer.AbstractOrderRepository;
import com.baofu.paycore.manager.storer.FreezeOrderRepository;
import com.google.common.collect.Lists;
import com.system.commons.utils.BeanCopyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * </p>
 * User:ames wang  Date: 2017/6/22
 */
@Component
public class FreezeOrderRepositoryImpl extends AbstractOrderRepository<FreezeBO>
        implements FreezeOrderRepository {

    @Autowired
    private FreezeMapper freezeMapper;

    @Override
    public FreezeBO lock(String requestOrderNo, String requestBizNo, String requestSystem) {
        return null;
    }

    @Override
    public FreezeBO load(String payNo) {
        FreezeDO freezeDO=freezeMapper.selectByPayNo(payNo);
        if(freezeDO==null){
            return null;
        }
        FreezeBO freezeBO=new FreezeBO();
        BeanUtils.copyProperties(freezeDO,freezeBO);
        freezeBO.setOperator(freezeDO.getCreatedBy());
        return freezeBO;
    }

    @Override
    public FreezeBO checkIdempotent(String requestBizNo, String requestOrderNo, String requestSystem) {
        FreezeDO freezeDO=freezeMapper.selectByOrderNo(requestOrderNo,requestBizNo,requestSystem);
        if(freezeDO==null){
            return null;
        }
        FreezeBO freezeBO=new FreezeBO();
        BeanUtils.copyProperties(freezeDO,freezeBO);
        freezeBO.setOperator(freezeDO.getCreatedBy());
        freezeBO.setErrorMsg(freezeDO.getErrorReason());
        return freezeBO;
    }

    @Override
    protected List<BaseDO> convertToDO(FreezeBO payBaseBO) {
        List<BaseDO> list = Lists.newArrayList();
        FreezeDO freezeDO=new FreezeDO();
        BeanUtils.copyProperties(payBaseBO,freezeDO);
        freezeDO.setErrorReason(payBaseBO.getErrorMsg());
        list.add(freezeDO);
        return list;
    }

    /**
     * 更新解冻单信息
     *
     * @param freezeBO 解冻单
     */
    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT,
            propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void updateUnfreeze(FreezeBO freezeBO) {
        FreezeDO freezeDO = BeanCopyUtil.objConvert(freezeBO,FreezeDO.class);
        freezeMapper.update(freezeDO);
        FreezeDO authDO = new FreezeDO();
        authDO.setPayNo(freezeBO.getOrigPayNo());
        authDO.setDealAmt(freezeBO.getAmt());
        freezeMapper.updateDealAmt(authDO);
    }
}
